package tpo.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import dto.Dto;
import org.apache.commons.io.FilenameUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import pojo.BuyOrder;
import pojo.Journal;
import pojo.Pay_order;
import pojo.Receipt_order;
import pojo.finance.*;
import tpo.client.FtpUtil;
import tpo.service.Receipt_oreder_Service_consuner;

import javax.annotation.Resource;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.*;

@RestController
//@RequestMapping(value = "/finance")
public class Receipt_order_consumer_Controller {

    @Resource
    Receipt_oreder_Service_consuner receipt_oreder_service_consuner;

    @RequestMapping("/allreceipt_order")
    public Dto<PageInfo<Receipt_order>> allreceipt_order(@RequestBody Receipt_canshu receipt_canshu){
        Map<String,Object> map=new HashMap();
        map.put("pageNun",receipt_canshu.getPageNun());
        map.put("pageSize",receipt_canshu.getPageSize());
        if(receipt_canshu.getAccount()!=null){
            map.put("account",receipt_canshu.getAccount());
        }else{
            map.put("account",null);
        }
        if(receipt_canshu.getReceiptSta()!=null){
            map.put("receiptSta",receipt_canshu.getReceiptSta());
        }else{
            map.put("receiptSta",null);
        }
        if(receipt_canshu.getTecha()!=null){
            map.put("techa",receipt_canshu.getTecha());
        }else{
            map.put("techa",null);
        }


        return receipt_oreder_service_consuner.allreceipt_order(map);
    }

    @RequestMapping(value=("/allReceipt_order"),produces="application/json;charset=UTF-8")
    public Dto<List<Receipt_order>> allReceipt_order(){
//        response.setContentType("text/html;charset=utf-8");
        return receipt_oreder_service_consuner.allReceipt_order();
    }

    @RequestMapping(value =("/selectReceipt_order"))
    public Dto<Receipt_order> selectReceipt_order(@RequestParam(value = "code") String code){
        return receipt_oreder_service_consuner.selectReceipt_order(code);
    }

    @RequestMapping(value = ("/selectallReceipt_ordermoney"))
    public Dto<BigDecimal> selectallReceipt_ordermoney(@RequestParam(value = "code") String code){
        return receipt_oreder_service_consuner.selectallReceipt_ordermoney(code);
    }

    /**收款单新增及文件上传操作*/
    @RequestMapping(value = ("/addReceipt_order"))
    public Dto addReceipt_order(@RequestParam(value = "file", required = false) MultipartFile file,
                       @RequestParam(value = "json", required = false) String json){
        Receipt_order receipt_order= JSON.parseObject(json,Receipt_order.class);
        if (file!=null){
            FtpUtil ftpUtil=new FtpUtil();
            String oldername=file.getOriginalFilename();
            String suffix= FilenameUtils.getExtension(oldername);
            String newname= UUID.randomUUID().toString().replace("-", "").substring(15).toLowerCase()+ "."+suffix;
            System.out.println(newname);
            FtpUtil.FtpConfig ftpConfig=new FtpUtil.FtpConfig();
            ftpConfig.setIp("192.168.2.80");
            ftpConfig.setPort(21);
            ftpConfig.setUserName("ftpuser");
            ftpConfig.setPassword("123456");
            try {
                ftpUtil.upload(ftpConfig,
                        "/tpo/",
                        file.getInputStream(),
                        newname);
            } catch (IOException e) {
                e.printStackTrace();
            }
            receipt_order.setVoucher(newname);
//            receipt_order.setReceTime(new Date());
        }
        receipt_order.setCode("rr-"+Num());
        System.out.println("收款单号"+receipt_order.getCode());
        Dto dto=receipt_oreder_service_consuner.addReceipt_order(receipt_order);
        return dto;
    }



    /**收款单作废操作*/
    @RequestMapping(value = ("/zuofei"))
    public Dto zuofei(@RequestBody Map<String,Object> map){
//        System.out.println("进来了");
        Dto dto =receipt_oreder_service_consuner.zuofei(map);
//        System.out.println("参数为:"+dto.getObj().toString());
        return dto;
    }


    /**收款单审核操作*/
    @RequestMapping("/shenhe")
    public Dto shenhe(@RequestBody Map<String,Object> map){
         return receipt_oreder_service_consuner.shenhe(map);
    }


    /**收支流水单分页查询*/
    @RequestMapping(value = "/alljournal")
    public Dto<PageInfo<Journal>> alljournal(@RequestBody Map<String,Object> map){
        return receipt_oreder_service_consuner.alljournal(map);
    }

    /**用于查询全部付款单*/
    @RequestMapping("/allPay_order")
    public Dto<PageInfo<Fukuandan>> allPay_order(@RequestBody Map<String,Object> map) {
        return receipt_oreder_service_consuner.allPay_order(map);
    }


    /**付款单新增及文件上传操作*/
    @RequestMapping(value = ("/addPay_order"))
    public Dto addPay_order(@RequestParam(value = "file", required = false) MultipartFile file,
                                @RequestParam(value = "json", required = false) String json){
        Pay_order pay_order= JSON.parseObject(json,Pay_order.class);
        if (file!=null){
            FtpUtil ftpUtil=new FtpUtil();
            String oldername=file.getOriginalFilename();
            String suffix= FilenameUtils.getExtension(oldername);
            String newname= UUID.randomUUID().toString().replace("-", "").substring(15).toLowerCase()+ "."+suffix;
            FtpUtil.FtpConfig ftpConfig=new FtpUtil.FtpConfig();
            ftpConfig.setIp("192.168.2.80");
            ftpConfig.setPort(21);
            ftpConfig.setUserName("ftpuser");
            ftpConfig.setPassword("123456");
            try {
                ftpUtil.upload(ftpConfig,
                        "/tpo/",
                        file.getInputStream(),
                        newname);
            } catch (IOException e) {
                e.printStackTrace();
            }
            pay_order.setVoucher(newname);


//            pay_order.setPayDate(new Date());
//            System.out.println("收款单号"+pay_order.getCode());
        }
        pay_order.setCode("po-"+Num());
        Dto dto=receipt_oreder_service_consuner.addPay_order(pay_order);
        return dto;
    }


    /**用于付款单作废操作*/
    @RequestMapping("/fukuanzuofei")
    public  Dto fukuanzuofei(@RequestBody Map<String,Object> map){
        return receipt_oreder_service_consuner.fukuanzuofei(map);
    }

    /**根据某一付款单的code查询某一付款单信息*/
    @RequestMapping("/selectpay_order")
    public Dto selectpay_order(@RequestParam(value = "code") String code){
        return receipt_oreder_service_consuner.selectpay_order(code);
    }

    /**用于付款审核*/
    @RequestMapping("/fukuanshenhe")
     public Dto fukuanshenhe(@RequestBody Map<String,Object>map){
        return receipt_oreder_service_consuner.fukuanshenhe(map);
    }

    /**用于客户对账的客户查询及其金额计算*/
    @RequestMapping("/selectusermoney")
    public Dto<PageInfo<Kehuduizhang>> selectusermoney(@RequestBody Map<String,Object> map){
     return receipt_oreder_service_consuner.selectusermoney(map);
    }

    /**用于客户对账页面的总计*/
    @RequestMapping("/selectusersum")
    public Dto<Kehuduizhang> selectusersum(){
        return receipt_oreder_service_consuner.selectusersum();
    }

    /**用于客户对账详情页面的总计*/
    @RequestMapping("/selectoneusersum")
    public Dto<Kehuduizhang> selectoneusersum(@RequestParam(value = "id") String id){
        return receipt_oreder_service_consuner.selectoneusersum(id);
    }

    /**用于客户对账详情页面的用户所属单据显示*/
    @RequestMapping("/selectoneuseralldan")
    public Dto<PageInfo<BuyOrder>> selectoneuseralldan(@RequestBody Map<String,Object> map){
        return receipt_oreder_service_consuner.selectoneuseralldan(map);
    }

    /**用于资金账户页面的某个时间段的收支帐*/
    @RequestMapping("/selectdangtianzhang")
    public Dto<PageInfo<Zijinduizhang>> selectdangtianzhang(@RequestBody Map<String,Object> map){
        return receipt_oreder_service_consuner.selectdangtianzhang(map);
    }

    /**用于资金账户明细*/
    @RequestMapping("/selectzijinmingxi")
    public Dto<PageInfo<Journal>> selectzijinmingxi(@RequestBody Map<String,Object> map){
        return receipt_oreder_service_consuner.selectzijinmingxi(map);
    }

    /**用于资金账户的7天内资金收支记录折线图*/
    @RequestMapping("/zijinzhexiantu")
    public Dto<List<Day>> zijinzhexiantu(){
        return receipt_oreder_service_consuner.zijinzhexiantu();
    }

    /**根据code查询某一订单的所有收款单 */
    @RequestMapping("/codeselectallreceipt")
    public Dto<List<Receipt_order>> codeselectallreceipt(@RequestParam(value = "code") String code){
     return receipt_oreder_service_consuner.codeselectallreceipt(code);
    }

    /**根据code查询某一订单的所有付款单 */
    @RequestMapping("/codeselectallfukuandan")
    public Dto<List<Pay_order>> codeselectallfukuandan(@RequestParam(value = "code") String code){
        return receipt_oreder_service_consuner.codeselectallfukuandan(code);
    }

    /**用于查询状态为已完成状态的付款单*/
    @RequestMapping("/selectallokfukuandan")
    public Dto selectallokfukuandan(@RequestParam(value = "code")String code){
        return receipt_oreder_service_consuner.selectallokfukuandan(code);
    }

    /**修改付款单上传凭证*/
    @RequestMapping("upPay_order")
//    Dto upPay_order(@RequestBody Map<String,Object> map){
    public Dto upPay_order(@RequestParam(value = "file", required = false) MultipartFile file,
                @RequestParam(value = "json", required = false) String json){
        Map<String,Object> map=JSON.parseObject(json,Map.class);
            if (file!=null){
                FtpUtil ftpUtil=new FtpUtil();
                String oldername=file.getOriginalFilename();
                String suffix= FilenameUtils.getExtension(oldername);
                String newname= UUID.randomUUID().toString().replace("-", "").substring(15).toLowerCase()+ "."+suffix;
                FtpUtil.FtpConfig ftpConfig=new FtpUtil.FtpConfig();
                ftpConfig.setIp("192.168.2.80");
                ftpConfig.setPort(21);
                ftpConfig.setUserName("ftpuser");
                ftpConfig.setPassword("123456");
                try {
                    ftpUtil.upload(ftpConfig,
                            "/tpo/",
                            file.getInputStream(),
                            newname);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                map.put("voucher",newname);
            }
            Dto dto=receipt_oreder_service_consuner.upPay_order(map);
            return dto;
        }
//    };
















    //随机生成6位数
    public static int Num() {
        int[] array = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
        Random rand = new Random();
        for (int i = 10; i > 1; i--) {
            int index = rand.nextInt(i);
            int tmp = array[index];
            array[index] = array[i - 1];
            array[i - 1] = tmp;
        }
        int result = 0;
        for (int i = 0; i < 6; i++) {
            result = result * 10 + array[i];
        }
        if (String.valueOf(result).length() == 6) {
            return result;
        } else {
            return Num();
        }
    }


}
